package com.heima.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.domain.pojo.QcCheckItem;
import com.heima.domain.pojo.QcCheckType;
import com.heima.domain.response.Result;
import com.heima.service.CheckItemService;
import com.heima.service.CheckTypeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "检验项目管理")
@RestController
@RequiredArgsConstructor
@RequestMapping("/qc/checkItem")
public class CheckItemController {

    @Autowired
    private  CheckItemService checkItemService;


    @Operation(summary = "分页查询检验项目", description = "根据条件分页查询检验项目")
    @GetMapping("/page")
    public Result<IPage<QcCheckItem>> getCheckItemPage(
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") Integer pageNum,
            @Parameter(description = "每页条数") @RequestParam(defaultValue = "10") Integer pageSize,
            // 新增：接收前端可能传递的查询条件（无则为 null）
            @Parameter(description = "名称（可选）") @RequestParam(required = false) String name) {

        Page<QcCheckItem> page = new Page<>(pageNum, pageSize);
        IPage<QcCheckItem> resultPage = checkItemService.queryPage(page,name);
        return Result.ok(resultPage);
    }

    @Operation(summary = "新增检验项目", description = "创建新的检验项目")
    @PostMapping
    public Result<Boolean> addCheckItem(
            @Parameter(description = "检验项目", required = true) @RequestBody QcCheckItem qcCheckItem) {

        boolean result = checkItemService.save(qcCheckItem);
        return Result.ok(result);
    }

    @Operation(summary = "查询检验项目", description = "根据ID查询检验项目")
    @GetMapping("/{id}")
    public Result<QcCheckItem> getCheckItemId(
            @Parameter(description = "检验项目ID", required = true) @PathVariable Long id) {

        QcCheckItem checkItem = checkItemService.getById(id);
        return Result.ok(checkItem);
    }


    @Operation(summary = "修改检验项目", description = "更新检验项目")
    @PutMapping
    public Result<Boolean> updateCheckItem(
            @Parameter(description = "检验项目", required = true) @RequestBody QcCheckItem qcCheckItem) {
        boolean result = checkItemService.updateById(qcCheckItem);
        return Result.ok(result);
    }

    @Operation(summary = "删除检验项目", description = "传入ID或ID集合进行删除")
    @DeleteMapping("/delete")
    public Result<Boolean> deleteCheckItems(@Parameter(description = "检验项目ID或ID集合") @RequestBody List<Long> ids) {
        if (ids == null || ids.isEmpty()) {
            return Result.fail("请传入要删除的ID");
        }

        boolean result = checkItemService.removeByIds(ids);

        if (result) {
            String msg = ids.size() == 1 ? "删除成功" : "批量删除成功（共" + ids.size() + "条）";
            return Result.ok(true, msg);
        } else {
            return Result.fail("删除失败");
        }
    }


}
